package com.smarthealthring.mm.ble;

import android.bluetooth.BluetoothGattCharacteristic;
import android.util.Log;

import com.clj.fastble.BleManager;
import com.smarthealthring.mm.utils.HexUtils2;
import com.smarthealthring.mm.utils.RxBus11;
import com.smarthealthring.mm.utils.RxBus;
import com.smarthealthring.mm.utils.RxBusBaseMessage;
import com.smarthealthring.mm.utils.SPUtils;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.utils.HexUtil;

/**
 * Created by joash on 2018/5/12.
 * <p>
 * 处理来自手环的数据
 */

public class ProcessNtfData {

    private ProcessNtfData() {
    }

    private static ProcessNtfData single = null;

    //静态工厂方法
    public static ProcessNtfData getInstance() {
        if (single == null) {
            single = new ProcessNtfData();
        }
        return single;
    }

    //    List<SyncDataBean.Step> stepList;
//    List<SyncDataBean.HR> hrList;
//    List<SyncDataBean.Sleep> sleepList;
    int power;
    String fwver;

    /**
     * 处理bond返回数据
     */
    public void processBond(BleDevice bleDevice, BluetoothGattCharacteristic characteristic, byte[] txValue) {

        Log.e("txValue===", HexUtil.formatHexString(txValue));

        if (BdProtocol.mReceivingFlag == false) { // 空闲状态

            processACK(txValue);

        } else {    // 分包接收命令

            for (int i = 0; i < txValue.length; i++)
                BdProtocol.mReceiveBuffer[BdProtocol.mReceiveingLen + i] = txValue[i];

            BdProtocol.mReceiveingLen += txValue.length;
            Log.e("mRecL---mLen", BdProtocol.mReceiveingLen + "---" + BdProtocol.mLenToReceive);

            if (BdProtocol.mReceiveingLen == BdProtocol.mLenToReceive) // 接收完成
            {
                // 接收到了完整的命令后，应该先进行ACK回应，对整个指令的CRC进行校验，如果正确就返回ACK，否则返回NACK

                int seqID = (BdProtocol.mReceiveBuffer[6] << 8) + (BdProtocol.mReceiveBuffer[7] << 0);

                Log.e("seqId===", seqID + "");
                Log.e("mReceiveBuffer===", HexUtil.formatHexString(BdProtocol.mReceiveBuffer));

                if (BdProtocol.checkCRC(BdProtocol.mReceiveBuffer)) {
                    Log.e("checkCRC===", "check_success");
                    BdProtocol.mCmdSeq = seqID;
                    BdProtocol.emitData(bleDevice, BdProtocol.ACK_CHANNEL);

                } else {
                    Log.e("checkCRC===", "check_fail");
                }

                // 开始解析指令
                byte cmd;
                byte key;

                cmd = BdProtocol.mReceiveBuffer[8];
                key = BdProtocol.mReceiveBuffer[10];

                if (cmd == (byte) 0x03) {
                    if (key == (byte) 0x02) {
                        Log.e("是绑定===", "in...");
                        // 解析步数数据
                        byte bondState = BdProtocol.mReceiveBuffer[12]; // 绑定返回命令的VALUE长度为1字节，表示绑定是否成功
                        Log.e("bondState", (bondState & 0XFF) + "");
                        if ((bondState & 0XFF) == 1) {
                            Log.e("绑定成功===", "in...");
                            // 修改绑定状态
                            SPUtils.setInt(SPUtils.BOND_STATE, 1);
                            SPUtils.setInt(SPUtils.IS_FIRST_BOND, 1);
                            // 发送跳转到主界面
                            RxBus.getInstance().send(RtEvent.BOND_SUCCESS_EVENT, new RxBusBaseMessage());
//                            BdProtocol.syncData(bleDevice);

                        } else
                            Log.e("绑定失败===", "in...");
                    }
                }
                BdProtocol.mReceivingFlag = false; // 置为空间状态，重新等待接收指令
            }
        }
    }

    /**
     * 处理步数返回数据
     */
    public void processStep(BleDevice bleDevice, BluetoothGattCharacteristic characteristic, byte[] txValue) {

        Log.e("processStep_txValue===", HexUtil.formatHexString(txValue));
        Log.e("mReceivingFlag===", BdProtocol.mReceivingFlag + "");

        if (BdProtocol.mReceivingFlag == false) { // 空闲状态

            processACK(txValue);
            BdProtocol.channel = BdProtocol.STEP_CHANNEL;

        } else {    // 分包接收命令

            for (int i = 0; i < txValue.length; i++)
                BdProtocol.mReceiveBuffer[BdProtocol.mReceiveingLen + i] = txValue[i];

            BdProtocol.mReceiveingLen += txValue.length;
            Log.e("mRecL---mLen", BdProtocol.mReceiveingLen + "---" + BdProtocol.mLenToReceive);

            if (BdProtocol.mReceiveingLen == BdProtocol.mLenToReceive) {
//                stepList = new ArrayList<>();
                // 接收完成
                int seqID = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 12, 16);
                Log.e("mReceiveBuffer===", HexUtil.formatHexString(BdProtocol.mReceiveBuffer));

                BdProtocol.mReceivingFlag = false; // 置为空间状态，重新等待接收指令
                if (BdProtocol.checkCRC(BdProtocol.mReceiveBuffer)) {
                    BdProtocol.mCmdSeq = seqID;
                    Log.e("checkcrc_success===", "发送步数ACK" + BdProtocol.mCmdSeq);
                    BdProtocol.emitData(bleDevice, BdProtocol.ACK_CHANNEL);
                } else {
                    Log.e("checkcrc_fail===", "check_fail");
                }

                // 开始解析指令
                byte cmd;
                byte key;

                cmd = BdProtocol.mReceiveBuffer[8];
                key = BdProtocol.mReceiveBuffer[10];

                if (cmd == (byte) 0x05) {
                    if (key == (byte) 0x42) {
                        Log.e("是步数===", "in...");
                        // 解析步数数据
                        int data_len = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 22, 26); //取得长度
                        Log.e("data_len===", data_len + "");
                        if (data_len == 160) {
                            BdProtocol.channel = BdProtocol.STEP_CHANNEL;
                        } else {
                            BdProtocol.channel = BdProtocol.HR_CHANNEL;
                        }

                        for (int i = 0; i < data_len / 8; i++) {
                            //前4个字节时间戳
                            //第5个字节
                            //第678个字节具体数据
                            int k = i * 16;
                            long time_stamp = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 26 + k, 34 + k);
//                            int status = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 34 + k, 36 + k);
                            String status = HexUtil.formatHexString(BdProtocol.mReceiveBuffer).substring(34 + k, 36 + k);
                            int sport_data = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 36 + k, 42 + k);

                            Log.e("sport_data===", k + "：" + time_stamp + "--" + status + "--" + sport_data);
                            BdProtocol.stepList.add(new SyncDataBean.Step(time_stamp, sport_data, status));

                        }
                        Log.e("步数处理完毕", "==============================");
                    }
                }

                BdProtocol.emitData(bleDevice, BdProtocol.channel);

            } else {
                //没接受完
                BdProtocol.channel = BdProtocol.STEP_CHANNEL;
            }

        }
    }


    /**
     * 处理心率返回数据
     */
    public void processHR(BleDevice bleDevice, BluetoothGattCharacteristic characteristic, byte[] txValue) {

        Log.e("processHR_txValue===", HexUtil.formatHexString(txValue));
        Log.e("mReceivingFlag===", BdProtocol.mReceivingFlag + "");

        if (BdProtocol.mReceivingFlag == false) { // 空闲状态

            processACK(txValue);
            BdProtocol.channel = BdProtocol.HR_CHANNEL;

        } else {    // 分包接收命令

            for (int i = 0; i < txValue.length; i++)
                BdProtocol.mReceiveBuffer[BdProtocol.mReceiveingLen + i] = txValue[i];

            BdProtocol.mReceiveingLen += txValue.length;
            Log.e("mRecL---mLen", BdProtocol.mReceiveingLen + "---" + BdProtocol.mLenToReceive);

            if (BdProtocol.mReceiveingLen == BdProtocol.mLenToReceive) // 接收完成
            {
//                hrList = new ArrayList<>();
                // 接收到了完整的命令后，应该先进行ACK回应，对整个指令的CRC进行校验，如果正确就返回ACK，否则返回NACK

                int seqID = (BdProtocol.mReceiveBuffer[6] << 8) + (BdProtocol.mReceiveBuffer[7] << 0);
                Log.e("mReceiveBuffer===", HexUtil.formatHexString(BdProtocol.mReceiveBuffer));

                BdProtocol.mReceivingFlag = false; // 置为空间状态，重新等待接收指令
                if (BdProtocol.checkCRC(BdProtocol.mReceiveBuffer)) {

                    BdProtocol.mCmdSeq = seqID;
                    Log.e("checkcrc_success===", "发送心率ACK" + BdProtocol.mCmdSeq);
                    BdProtocol.emitData(bleDevice, BdProtocol.ACK_CHANNEL);
                } else {
                    Log.e("checkcrc_fail===", "check_fail");
                }

                // 开始解析指令
                byte cmd;
                byte key;

                cmd = BdProtocol.mReceiveBuffer[8];
                key = BdProtocol.mReceiveBuffer[10];

                if (cmd == (byte) 0x05) {
                    if (key == (byte) 0x44) {
                        Log.e("是心率===", "in...");
                        // 解析步数数据
                        int data_len = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 22, 26); //取得长度
                        Log.e("data_len===", data_len + "");

                        if (data_len == 100) {
                            BdProtocol.channel = BdProtocol.HR_CHANNEL;
                        } else {
                            BdProtocol.channel = BdProtocol.SLEEP_CHANNEL;
                        }

                        for (int i = 0; i < data_len / 5; i++) {
                            //前4个字节时间戳
                            //第5个字节
                            //第678个字节具体数据
                            int k = i * 10;
                            long time_stamp = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 26 + k, 34 + k);
                            int hr_data = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 34 + k, 36 + k);

                            Log.e("hr_data===", k + "：" + time_stamp + "--" + hr_data);
                            BdProtocol.hrList.add(new SyncDataBean.HR(time_stamp, hr_data));


                        }
                        Log.e("心率处理完毕", "==============================");
                    }
                }

                BdProtocol.emitData(bleDevice, BdProtocol.channel);

            } else {
                //没接受完
                BdProtocol.channel = BdProtocol.HR_CHANNEL;
            }
        }

    }

    /**
     * 处理睡眠返回数据
     */
    public void processSleep(BleDevice bleDevice, BluetoothGattCharacteristic characteristic, byte[] txValue) {

        Log.e("processSleep_txValue===", HexUtil.formatHexString(txValue));
        Log.e("mReceivingFlag===", BdProtocol.mReceivingFlag + "");

        if (BdProtocol.mReceivingFlag == false) { // 空闲状态

            processACK(txValue);
            BdProtocol.channel = BdProtocol.SLEEP_CHANNEL;

        } else {    // 分包接收命令

            for (int i = 0; i < txValue.length; i++)
                BdProtocol.mReceiveBuffer[BdProtocol.mReceiveingLen + i] = txValue[i];

            BdProtocol.mReceiveingLen += txValue.length;
            Log.e("mRecL---mLen", BdProtocol.mReceiveingLen + "---" + BdProtocol.mLenToReceive);

            if (BdProtocol.mReceiveingLen == BdProtocol.mLenToReceive) // 接收完成
            {
                // 接收到了完整的命令后，应该先进行ACK回应，对整个指令的CRC进行校验，如果正确就返回ACK，否则返回NACK
//                sleepList = new ArrayList<>();

                int seqID = (BdProtocol.mReceiveBuffer[6] << 8) + (BdProtocol.mReceiveBuffer[7] << 0);
                Log.e("mReceiveBuffer===", HexUtil.formatHexString(BdProtocol.mReceiveBuffer));

                BdProtocol.mReceivingFlag = false; // 置为空间状态，重新等待接收指令
                if (BdProtocol.checkCRC(BdProtocol.mReceiveBuffer)) {

                    BdProtocol.mCmdSeq = seqID;
                    Log.e("checkcrc_success===", "发送睡眠ACK" + BdProtocol.mCmdSeq);
                    BdProtocol.emitData(bleDevice, BdProtocol.ACK_CHANNEL);
                } else {
                    Log.e("checkcrc_fail===", "check_fail");
                }

                // 开始解析指令
                byte cmd;
                byte key;

                cmd = BdProtocol.mReceiveBuffer[8];
                key = BdProtocol.mReceiveBuffer[10];

                if (cmd == (byte) 0x05) {
                    if (key == (byte) 0x46) {
                        Log.e("是睡眠===", "in...");
                        // 解析睡眠数据
                        int data_len = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 22, 26); //取得长度
                        Log.e("data_len===", data_len + "");
                        if (data_len == 140) {
                            BdProtocol.channel = BdProtocol.SLEEP_CHANNEL;
                        } else {
//                            BdProtocol.channel = BdProtocol.OVER_CHANNEL;
                            BdProtocol.channel = BdProtocol.POWER_CHANNEL;
                        }

                        for (int i = 0; i < data_len / 7; i++) {
                            //前4个字节时间戳
                            //第5个字节
                            //第678个字节具体数据
                            int k = i * 14;
                            long time_stamp = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 26 + k, 34 + k);
//                            int status = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 34 + k, 36 + k);
                            String status = HexUtil.formatHexString(BdProtocol.mReceiveBuffer).substring(34 + k, 36 + k);
                            int sleep_data = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 36 + k, 40 + k);

                            Log.e("sleep_data===", k + "：" + time_stamp + "--" + status + "--" + sleep_data);
                            BdProtocol.sleepList.add(new SyncDataBean.Sleep(time_stamp, status));

                        }
                        Log.e("睡眠处理完毕", "==============================");

                    }
                }

                BdProtocol.emitData(bleDevice, BdProtocol.channel);

            } else {
                BdProtocol.channel = BdProtocol.SLEEP_CHANNEL;
            }
        }
    }

    /**
     * 处理电量返回数据
     */
    public void processPower(BleDevice bleDevice, BluetoothGattCharacteristic characteristic, byte[] txValue) {

        Log.e("processPower_txValue===", HexUtil.formatHexString(txValue));
        Log.e("mReceivingFlag===", BdProtocol.mReceivingFlag + "");

        if (BdProtocol.mReceivingFlag == false) { // 空闲状态

            processACK(txValue);
            BdProtocol.channel = BdProtocol.POWER_CHANNEL;

        } else {    // 分包接收命令

            for (int i = 0; i < txValue.length; i++)
                BdProtocol.mReceiveBuffer[BdProtocol.mReceiveingLen + i] = txValue[i];

            BdProtocol.mReceiveingLen += txValue.length;
            Log.e("mRecL---mLen", BdProtocol.mReceiveingLen + "---" + BdProtocol.mLenToReceive);

            if (BdProtocol.mReceiveingLen == BdProtocol.mLenToReceive) // 接收完成
            {
                // 接收到了完整的命令后，应该先进行ACK回应，对整个指令的CRC进行校验，如果正确就返回ACK，否则返回NACK

                int seqID = (BdProtocol.mReceiveBuffer[6] << 8) + (BdProtocol.mReceiveBuffer[7] << 0);
                Log.e("mReceiveBuffer===", HexUtil.formatHexString(BdProtocol.mReceiveBuffer));

                BdProtocol.mReceivingFlag = false; // 置为空间状态，重新等待接收指令
                if (BdProtocol.checkCRC(BdProtocol.mReceiveBuffer)) {

                    BdProtocol.mCmdSeq = seqID;
                    Log.e("checkcrc_success===", "发送电量ACK" + BdProtocol.mCmdSeq);
                    BdProtocol.emitData(bleDevice, BdProtocol.ACK_CHANNEL);
                } else {
                    Log.e("checkcrc_fail===", "check_fail");
                }

                // 开始解析指令
                byte cmd;
                byte key;

                cmd = BdProtocol.mReceiveBuffer[8];
                key = BdProtocol.mReceiveBuffer[10];

                if (cmd == (byte) 0x02) {
                    if (key == (byte) 0x09) {
                        BdProtocol.channel = BdProtocol.FWVER_CHANNEL;
                        Log.e("是电量===", "in...");
                        // 解析电量数据
                        power = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 26, 28); //取得长度
                        SPUtils.setInt(SPUtils.DEVICE_POWER, power);
                        Log.e("电量处理完毕", "==============================");

                    }
                }

                BdProtocol.emitData(bleDevice, BdProtocol.channel);

            } else {
                BdProtocol.channel = BdProtocol.POWER_CHANNEL;
            }
        }
    }

    /**
     * 处理固件版本返回数据
     */
    public void processFwver(BleDevice bleDevice, BluetoothGattCharacteristic characteristic, byte[] txValue) {

        Log.e("processFwver_txValue===", HexUtil.formatHexString(txValue));
        Log.e("mReceivingFlag===", BdProtocol.mReceivingFlag + "");

        if (BdProtocol.mReceivingFlag == false) { // 空闲状态

            processACK(txValue);
            BdProtocol.channel = BdProtocol.FWVER_CHANNEL;

        } else {    // 分包接收命令

            for (int i = 0; i < txValue.length; i++)
                BdProtocol.mReceiveBuffer[BdProtocol.mReceiveingLen + i] = txValue[i];

            BdProtocol.mReceiveingLen += txValue.length;
            Log.e("mRecL---mLen", BdProtocol.mReceiveingLen + "---" + BdProtocol.mLenToReceive);

            if (BdProtocol.mReceiveingLen == BdProtocol.mLenToReceive) // 接收完成
            {
                // 接收到了完整的命令后，应该先进行ACK回应，对整个指令的CRC进行校验，如果正确就返回ACK，否则返回NACK

                int seqID = (BdProtocol.mReceiveBuffer[6] << 8) + (BdProtocol.mReceiveBuffer[7] << 0);
                Log.e("mReceiveBuffer===", HexUtil.formatHexString(BdProtocol.mReceiveBuffer));

                BdProtocol.mReceivingFlag = false; // 置为空间状态，重新等待接收指令
                if (BdProtocol.checkCRC(BdProtocol.mReceiveBuffer)) {

                    BdProtocol.mCmdSeq = seqID;
                    Log.e("checkcrc_success===", "发送固件版本ACK" + BdProtocol.mCmdSeq);
                    BdProtocol.emitData(bleDevice, BdProtocol.ACK_CHANNEL);
                } else {
                    Log.e("checkcrc_fail===", "check_fail");
                }

                // 开始解析指令
                byte cmd;
                byte key;

                cmd = BdProtocol.mReceiveBuffer[8];
                key = BdProtocol.mReceiveBuffer[10];

                if (cmd == (byte) 0x02) {
                    if (key == (byte) 0x0B) {
                        BdProtocol.channel = BdProtocol.OVER_CHANNEL;
                        Log.e("是固件版本===", "in...");
                        // 解析电量数据
                        int fwver_1 = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 26, 28); //取得长度
                        int fwver_2 = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 28, 30); //取得长度
                        int fwver_3 = HexUtils2.bytes2Int(BdProtocol.mReceiveBuffer, 30, 32); //取得长度
                        fwver = fwver_1 + "." + fwver_2 + "." + fwver_3;
                        SPUtils.setString(SPUtils.DEVICE_VERSION, fwver);
                        Log.e("固件版本处理完毕", "==============================");

                    }
                }

                BdProtocol.emitData(bleDevice, BdProtocol.channel);

                // 所有数据完毕
                if (BdProtocol.channel == BdProtocol.OVER_CHANNEL) {
                    Log.e("所有数据处理完毕", "==============================");
                    // 同步数据到服务器
                    SyncDataBean syncDataBean = new SyncDataBean(BdProtocol.stepList, BdProtocol.hrList, BdProtocol.sleepList, power, fwver);
                    RxBus.getInstance().send(SyncDataEvent.SYNC_DATA, syncDataBean);
                    Log.e("sync datacheck===", syncDataBean.getSteps().size() + "--"
                            + syncDataBean.getHrs().size() + "--" + syncDataBean.getSleeps().size() + "--" + syncDataBean.getPower() + "--" + syncDataBean.getFwver() + "--" + syncDataBean.getSteps().get(0).getSteps());

                    BdProtocol.sleepList.clear();
                    BdProtocol.stepList.clear();
                    BdProtocol.hrList.clear();
                    // 关闭通知
                    BdProtocol.stopNotify(bleDevice);

                }

            } else {
                BdProtocol.channel = BdProtocol.FWVER_CHANNEL;
            }
        }
    }


    /**
     * 解绑返回数据
     */
    public void processUnbind(BleDevice bleDevice, BluetoothGattCharacteristic characteristic, byte[] txValue) {

        Log.e("processUnb_txValue===", HexUtil.formatHexString(txValue));
        if (txValue[0] == (byte) 0xAB && txValue[1] == (byte) 0x11 && txValue.length == 8) {
            Log.e("解绑成功===", "...");
            SPUtils.setInt(SPUtils.BOND_STATE, 0);
//            BleManager.getInstance().disconnect(bleDevice);
            BdProtocol.stopNotify(bleDevice);
            // 发送跳转到主界面
            RxBus.getInstance().send(RtEvent.UNBOND_SUCCESS_EVENT, new RxBusBaseMessage());
        } else {
            Log.e("解绑失败===", "...");
        }

    }

    /**
     * 处理写入时间
     */
    public void processTime(BleDevice bleDevice, BluetoothGattCharacteristic characteristic, byte[] txValue) {

        Log.e("processTime_txValue===", HexUtil.formatHexString(txValue));
        if (txValue[0] == (byte) 0xAB && txValue[1] == (byte) 0x11 && txValue.length == 8) {
            Log.e("写入时间成功===", "...");
//            bleCallback.onSuccess();
            int bond_state = SPUtils.getInt(SPUtils.BOND_STATE, 0);
            if (bond_state == 0) {   //未绑定就绑定手环
                BdProtocol.startBond(bleDevice);
                SPUtils.setInt(SPUtils.BOND_STATE, 1);
            }
        } else {
            Log.e("写入时间失败===", "...");
//            bleCallback.onFailure();
        }

    }

    /**
     * 处理写入语言
     */
    public void processLan(BleDevice bleDevice, BluetoothGattCharacteristic characteristic, byte[] txValue) {

        Log.e("processLan_txValue===", HexUtil.formatHexString(txValue));
        if (txValue[0] == (byte) 0xAB && txValue[1] == (byte) 0x11 && txValue.length == 8) {
            Log.e("写入语言成功===", "...");
            // 关闭通知
            BdProtocol.stopNotify(bleDevice);
        } else {
            Log.e("写入语言失败===", "...");
//            bleCallback.onFailure();
        }

    }

    /**
     * 处理公共ACK
     */
    public static void processACK(byte[] txValue) {

        // 接收到的第一个字节为0XAB，即表示命令起始// 手环上的命令分包发送，第一包固定为8字节
        if (txValue[0] == (byte) 0xAB && txValue.length == 8) {

            BdProtocol.mReceivingFlag = true;
            BdProtocol.mReceiveingLen = 0;

            BdProtocol.mLenToReceive = HexUtils2.bytes2Int(txValue, 4, 8) + txValue.length;
            for (int i = BdProtocol.mReceiveingLen; i < txValue.length; i++)
                BdProtocol.mReceiveBuffer[i] = txValue[i];

            BdProtocol.mReceiveingLen = txValue.length;

            Log.e("mRecL---mLen", BdProtocol.mReceiveingLen + "---" + BdProtocol.mLenToReceive);

            if (BdProtocol.mReceiveingLen == BdProtocol.mLenToReceive)     // 只有一个包的指令，目前只有应答包是只有8个字节
            {
                if ((byte) BdProtocol.mReceiveBuffer[1] == 0x11) // 是否为ACK包
                {
//                        if (mCmdSeq == (mReceiveBuffer[6] << 8) + (mReceiveBuffer[7] << 0))  // 判断返回的ACK包是否为响应刚刚发送的指令
//                        {
//                            listAdapter.add("响应包正确，指令发送成功！");
//                        } else {
//                            listAdapter.add("响应包不对应！");
//                        }
                    Log.e("processACK===", "是应答包");

                    BdProtocol.mReceivingFlag = false; // 置为空间状态，重新等待接收指令
                }

            }
        }
    }
}
